package cn.wzvtc.demo.controller;

import cn.wzvtc.demo.common.dto.Result;
import cn.wzvtc.demo.model.entity.UserFormEntity;
import cn.wzvtc.demo.service.UserFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: 用户填写表单控制器
 * @author: Rex
 * @create: 2021-07-10 18:57
 */

@RestController
@RequestMapping("/user")
public class UserFormController {

  @Autowired
  UserFormService userFormService;


  /**
   * 提交表单
   * @param userFormEntity 用户填写的表单
   * @return 处理结果
   */
  @PostMapping("/form")
  public Result<UserFormEntity> add(@RequestBody UserFormEntity userFormEntity) {
    UserFormEntity res = userFormService.save(userFormEntity);
    System.out.println(userFormEntity.toString());
    return Result.success(res);
  }

  /**
   * 删除整个表单
   * @param id 表单编号
   * @return 处理结果
   */
  @DeleteMapping("/form")
  public Result<Void> deleteById(String id) {
    userFormService.deleteById(id);
    return Result.success("删除成功");
  }

  /**
   * 根据id和componentId删除组件
   * @param id 表单编号
   * @param componentId 组件编号
   * @return 处理结果
   */
  @DeleteMapping(value = "/component")
  public Result<Void> deleteByIdAndComponentId(String id,String componentId) {
    userFormService.deleteByIdAndComponentId(id,componentId);
    return Result.success("删除成功");
  }


  /**
   * 根据id查找整个数据
   * @param id 用户表单编号
   * @return 处理结果
   */
  @GetMapping(value = "/forms")
  public Result<UserFormEntity> findById(String id) {
    UserFormEntity userFormEntity = userFormService.findById(id);
    System.out.println(userFormEntity);
    return Result.success(userFormEntity);
  }

  /**
   * 根据id更新数据
   * @param userFormEntity 待更新表单
   * @return 处理结果
   */
  @PutMapping("/form")
  public Result<UserFormEntity> updateById(@RequestBody UserFormEntity userFormEntity) {
    userFormService.update(userFormEntity);
    System.out.println(userFormEntity);
    return Result.success(userFormEntity);
  }

}
